//
//  DataContext.m
//  addActionForm
//
//  Created by user on 06.05.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DataContext.h"


@implementation DataContext

@synthesize actionCollection;
@synthesize projectCollection;

- (id) init {
	char *baseDados = "/Users/user/Desktop/keyword-nova/Classes/Data/data1.sqlite";
	[super init];
	if (sqlite3_open(baseDados,  &database ) == SQLITE_OK)
	{		
		return self;
	}
	return nil;
}

- (void) insertNewProject : (Project *) project{
	[projectCollection addObject:project];
}

- (void) loadProjects{
	projectCollection = [[NSMutableArray alloc] init];
	[self getALLToArrayProjects:projectCollection];
}

- (void) unloadProjects{
	int conta = [projectCollection count];
	for(int i = 0; i < conta; i++)
	{
		Projects * project = [projectCollection objectAtIndex: 0];
		[projectCollection removeObjectAtIndex:0];
		
		[project dealloc];
		project = nil;
	}
	[projectCollection dealloc];
}

- (void) dealloc{
	[self unloadProjects];
	[super dealloc];
}

- (void) executeQuery : (NSString *) query {
	//sqlite3_stmt* actionStatement;
	//int result = sqlite3_prepare_v2(database,[query UTF8String], -1, &actionStatement, nil);
	char * error;
	int result = sqlite3_exec(database, [query UTF8String], nil, nil, &error);
	NSLog(query);
	if(result == SQLITE_OK)
	{

		NSLog(@"INSERTING THE RECORD TO DB .... SUCCESS");
	}
	else
	{
		NSLog(@"FAILED TO INSERT THE RECORD : %@", error);
	}
	//sqlite3_finalize(actionStatement);
}


/*
- (void) reloadActions{
	sqlite3_stmt *actionStatement;
	sqlite3_prepare_v2(database, "SELECT id, description FROM T_ACTION", -1, &actionStatement, nil);
	
	while(sqlite3_step(actionStatement) == SQLITE_ROW)
	{
		NSString *message = [[NSString alloc] initWithUTF8String:
							 (char *)sqlite3_column_text(actionStatement, 1)];
		
		int idAction = (int) sqlite3_column_int(actionStatement,0);
		Action *action = [ModelFactory CreateAction : message : idAction];	
		
		
		sqlite3_stmt *commentStatement;
		NSString *query = [NSString stringWithFormat:@"SELECT id,subject,body,id_action FROM T_ACTION_COMMENT  where id_action = %d",  idAction];
		sqlite3_prepare_v2(database,[query UTF8String] , -1, &commentStatement, nil);
		
		while(sqlite3_step(commentStatement) == SQLITE_ROW)
		{
			NSString *subject = [[NSString alloc] initWithUTF8String:
								 (char *)sqlite3_column_text(commentStatement, 1)];
			
			NSString *body = [[NSString alloc] initWithUTF8String:
							  (char *)sqlite3_column_text(commentStatement, 2)];
			
			//int idAction = (int) sqlite3_column_int(commentStatement,3);
			
			int idComment = (int) sqlite3_column_int(commentStatement,0);
			
			Comment *comment = [ModelFactory CreateActionComment : subject : body : idComment : action];
			
			[action addComment : comment];
		}	
		sqlite3_finalize(commentStatement);
		
		
		[actionCollection addObject:action];
	}	
	sqlite3_finalize(actionStatement);
}
*/

@end
